#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
LL toNum(char c)
{
   if (c >= '0' && c <= '9')
      return c - '0';
   return c - 'a' + 10;
}
LL toDecimal(string str, LL radix)
{
   LL num = 0;
   for (int i = 0; i < str.length(); i++)
   {
      num = num * radix + toNum(str[i]);
      if (num < 0)
         return -1;
   }
   return num;
}
int main()
{
   string a, b;
   int tag, radix;
   LL l, r, mid;
   cin >> a >> b >> tag >> radix;
   if (tag == 2)
   {
      swap(a, b);
   }
   LL base = toDecimal(a, radix);
   l = 2;
   r = base;
   for (int i = 0; i < b.size(); i++)
   {
      l = max(l, toNum(b[i] + 1));
   }
   while (r >= l)
   {
      mid = (l + r) >> 1;
      LL t = toDecimal(b, mid);
      if (t >= base || t == -1)
         r = mid - 1;
      else
         l = mid + 1;
   }
   if (toDecimal(b, l) == base)
      cout << l;
   else
      puts("Impossible");
   return 0;
}